package com.example.bobo.yuejian322.Activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bobo.yuejian322.Interfa.PaySeverice;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaomingActivity extends BaseActivity {
  private TextView peixunname,xunlianleixing,Price,num,bmxuzhi,zongjia;
    private EditText rename,telphone;
    private ImageView jiahao,jianhao;
    private Button jiesuan;
    private ImageButton back;
    private CheckBox weixin,zhifubao;
    private String Peixunname,bmnotice,Userid,trainID;

    private int bmPrice;
    private int shuliang_number =1;
    private int shulian  =1;
    private   double  Zhongjia;
    private  int  paytype=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baoming);


//获取传入的值
        Peixunname = getIntent().getStringExtra("peixunmane");
        bmPrice = getIntent().getIntExtra("bmPrice",-1);
        bmnotice = getIntent().getStringExtra("bmnotice");
        Userid=getIntent().getStringExtra("userid");
        trainID=getIntent().getStringExtra("Train_id");


        //绑到控件
        peixunname= (TextView) findViewById(R.id.peixunName);
        xunlianleixing= (TextView) findViewById(R.id.xunlianleixing);
        Price= (TextView) findViewById(R.id.price);
        num= (TextView) findViewById(R.id.number);
        bmxuzhi= (TextView) findViewById(R.id.bmxuzhi);
        zongjia= (TextView) findViewById(R.id.num);
        rename= (EditText) findViewById(R.id.rename);
        telphone= (EditText) findViewById(R.id.telphone);
        jiahao= (ImageView) findViewById(R.id.jiahao);
        jianhao= (ImageView) findViewById(R.id.jianhao);
        jiesuan= (Button) findViewById(R.id.button_jiesuan);
        weixin= (CheckBox) findViewById(R.id.weixin);
        zhifubao= (CheckBox) findViewById(R.id.zhifubao);
        back= (ImageButton) findViewById(R.id.pay_back);


//给控件加载数据
        peixunname.setText(Peixunname);
        Price.setText(bmPrice+"元/人");
        bmxuzhi.setText(bmnotice);
        xunlianleixing.setText(Peixunname);
        num.setText(shuliang_number+"");
        Zhongjia=bmPrice * shuliang_number;
        zongjia.setText("合计:"+Zhongjia+"元");

     //加减号的监听
        shuiang();
     //支付选择
        pay();

//获取总价
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(count > 0){
                    int str = Integer.valueOf(num.getText().toString());
                    double  suum = str * bmPrice;
                    Zhongjia=suum;
                    zongjia.setText("合计："+Zhongjia+"元");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


//结算按钮监听
        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               // Toast.makeText(BaomingActivity.this, shuliang_number+"", Toast.LENGTH_SHORT).show();

                 String Relname=rename. getText().toString();
                 String Telephone=telphone.getText().toString();
                if(Relname.equals("")||Telephone.equals(""))
                {
                    Toast.makeText(BaomingActivity.this, "请输入有效的真实姓名和联系方式", Toast.LENGTH_SHORT).show();
                    Toast.makeText(BaomingActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
                }
                 else{

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://172.24.10.175/workout/api.php/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                PaySeverice paySeverice =retrofit.create(PaySeverice.class);

                Call<String> listCall = paySeverice.pay(Relname,trainID,Telephone,shuliang_number,paytype,Zhongjia,trainID,"191",userId);

                listCall.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        response.body();
                        Toast.makeText(BaomingActivity.this, "报名成功", Toast.LENGTH_SHORT).show();
                       finish();
                    }
                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Toast.makeText(BaomingActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
                    }
                });

            }
            }
        });



//返回箭头
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  finish();
            }
        });


    }
    //加减号的监听
      private void shuiang(){


          jiahao.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {

                  shulian+=1;
                  if(shulian==30)
                  {

                      shulian=1;

                  }
                  shuliang_number = shulian;
                  num.setText(shuliang_number+"");
              }
          });


        jianhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(shulian>=1)
                {
                    shulian--;

                    shuliang_number = shulian;
                    num.setText(shuliang_number+"");
                }

            }
        });




      }

    //字符方式的选择
    private void pay(){

        weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b==true)
                {


                    paytype=1;

                    Toast.makeText(BaomingActivity.this,
                            "你选择了微信支付"    , Toast.LENGTH_LONG).show();
                }





            }
        });

     zhifubao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
             if(b==true)
             {

                 paytype=1;
                 Toast.makeText(BaomingActivity.this,
                         "你选择了支付宝支付"    , Toast.LENGTH_LONG).show();
             }
         }
     });









    }



}
